﻿using System;
using System.Collections;
using System.Collections.Generic;
using LCL.Serialization;
using System.Linq;
using System.IO;
namespace LCL.Config
{
    [Serializable]
    public class SubscribedEvent
    {
        public string EventType { get; set; }
        public string EventAssembly { get; set; }
    }
    /// <summary>
    /// Bus配置信息类文件
    /// </summary>
    [Serializable]
    public class BusConfigInfo : IConfigInfo
    {
        public SubscribedEvent PublishEvent { get; set; }
        public List<SubscribedEvent> SubscribedEvents { get; set; }
    }
    /// <summary>
    ///  Bus配置类
    /// </summary>
    public class BusConfigs
    {
        private static object lockhelper = new object();
        private static DateTime m_fileoldchange;//程序刚加载时.config文件修改时间
        private static DateTime m_filenewchange;//最近.config文件修改时间
        private static string m_configfilepath = Utils.GetMapPath("config/bus.config");
        private static List<BusConfigInfo> m_busConfigs = null;
        static BusConfigs()
        {
            Path.GetDirectoryName(m_configfilepath).CreateDir();
            if (m_configfilepath.FileExists())
            {
                SetConfigInfo();
                m_fileoldchange = System.IO.File.GetLastWriteTime(m_configfilepath);
            }
        }
        private static void SetConfigInfo()
        {
            var obj = new ObjectXmlSerializer();
            List<BusConfigInfo> list = (List<BusConfigInfo>)obj.Load(typeof(List<BusConfigInfo>), m_configfilepath);
            m_busConfigs = list;
        }
        /// <summary>
        /// 监视配置文件
        /// </summary>
        private static void FileMonitor()
        {
            if (m_configfilepath.FileExists())
            {
                //获取文件最近修改时间 
                m_filenewchange = System.IO.File.GetLastWriteTime(m_configfilepath);
                //当ftp.config修改时间发生变化时
                if (m_fileoldchange != m_filenewchange)
                {
                    lock (lockhelper)
                    {
                        if (m_fileoldchange != m_filenewchange)
                        {
                            //当文件发生修改(时间变化)则重新设置相关FTP信息对象
                            SetConfigInfo();
                            m_fileoldchange = m_filenewchange;
                        }
                    }
                }
            }
        }
        public static List<BusConfigInfo> Configs
        {
            get
            {
                FileMonitor();
                return m_busConfigs;
            }
        }
        public static bool Save(List<BusConfigInfo> list)
        {
            var obj = new ObjectXmlSerializer();
            List<BusConfigInfo> arr = (List<BusConfigInfo>)obj.Load(typeof(List<BusConfigInfo>), m_configfilepath);
            if (arr == null)
            {
                arr = new List<BusConfigInfo>();
            }
            arr.AddRange(list);
           return obj.Save(arr, m_configfilepath);
        }
        public static bool Save(BusConfigInfo info)
        {
            var obj = new ObjectXmlSerializer();
            List<BusConfigInfo> arr = (List<BusConfigInfo>)obj.Load(typeof(List<BusConfigInfo>), m_configfilepath);
            if (arr == null)
            {
                arr = new List<BusConfigInfo>();
            }
            arr.AddIfNotContains(info);
           return obj.Save(arr, m_configfilepath);
        }
    }
}
